﻿/*
 * Copyright (C) 2021. Huawei Device Co., Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the MIT License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * MIT License for more details.
 */


#include "UIDrawDashedLine.h"

namespace DuiLib {
UIDrawDashedLine::UIDrawDashedLine()
{
    pointNum = 0;
    // Initializing the GDI+ Environment
    Gdiplus::GdiplusStartup(&gdiToken, &gdiplusStartupInput, nullptr);
}
UIDrawDashedLine::~UIDrawDashedLine()
{
    // Shutting Down the GDI+ Environment
    Gdiplus::GdiplusShutdown(gdiToken);
}
void UIDrawDashedLine::DoPostPaint(HDC doPaint, const RECT& paintRect)
{
    if (!isVisible) {
        return;
    }
    DrawLine(doPaint);
}

void UIDrawDashedLine::DrawLine(HDC hDC)
{
    using namespace Gdiplus;
    const int minPostNum = 1;
    if (pointNum <= minPostNum) {
        return;
    }

    Graphics graphics(hDC);

    const int colorWhite = 255;
    Color color(colorWhite, colorWhite, colorWhite);
    const float lineFont = 1;
    Pen pen(color, lineFont);
    pen.SetDashStyle(DashStyleDash);
    const float lineInterval = 5.0f;
    const int arryNum = 2;
    REAL dashArry[arryNum] = { lineInterval, lineInterval };
    pen.SetDashPattern(dashArry, arryNum);
    pen.SetStartCap(LineCapRound);
    pen.SetEndCap(LineCapRound);
    graphics.SetSmoothingMode(SmoothingModeAntiAlias); // Smoothing
    const int next = 1;
    for (int i = 0; i < pointNum - next; i++) {
        graphics.DrawLine(&pen, pointPtr[i].x, pointPtr[i].y,
            pointPtr[i + next].x, pointPtr[i + next].y);
    }
}

void UIDrawDashedLine::SetPoint(POINT* pointArry, int points)
{
    if (pointArry == nullptr) {
        return;
    }
    pointNum = points;
    // A maximum of 10 points are supported.
    const int maxPoint = 10;
    if (pointNum > maxPoint) {
        pointNum = maxPoint;
    }

    for (int i = 0; i < pointNum; i++) {
        pointPtr[i] = pointArry[i];
    }
}

void UIDrawDashedLine::SetVisible(const bool visible)
{
    isVisible = visible;
}
}